package com.hjt.gulimall.member.service.impl;
import com.hjt.common.to.member.UserLoginTo;
import com.hjt.gulimall.member.exception.EmailHasExistedException;
import com.hjt.gulimall.member.exception.UserNameHasExistedException;
import com.hjt.gulimall.member.service.MemberLevelService;
import com.hjt.gulimall.member.vo.RegisterVo;
import com.hjt.gulimall.member.vo.UserLoginVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjt.common.utils.PageUtils;
import com.hjt.common.utils.Query;
import com.hjt.gulimall.member.dao.MemberDao;
import com.hjt.gulimall.member.entity.MemberEntity;
import com.hjt.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelService memberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public void register(RegisterVo registerVo) throws UserNameHasExistedException, EmailHasExistedException {
        MemberEntity memberEntity = new MemberEntity();
        // 检查用户名，邮箱是否唯一
        checkUserNameUnique(registerVo.getUserName());
        checkEmailUnique(registerVo.getEmail());
        memberEntity.setUsername(registerVo.getUserName());
        memberEntity.setEmail(registerVo.getEmail());
        // 密码加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        memberEntity.setPassword(encoder.encode(registerVo.getPassword()));
        // 设置默认等级
        Long levelId = memberLevelService.getDefaultLevelId();
        memberEntity.setLevelId(levelId);

        // 其他默认信息
        memberEntity.setGrowth(0);
        memberEntity.setStatus(0);
        memberEntity.setNickname(memberEntity.getUsername());
        baseMapper.insert(memberEntity);
    }

    @Override
    public UserLoginTo login(UserLoginVo loginVo) {
        String userAccount = loginVo.getUserAccount();
        MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("email", userAccount).or().eq("username", userAccount));
        if (memberEntity != null) {
            String password = memberEntity.getPassword();
            String userPassword = loginVo.getUserPassword();
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            if (encoder.matches(userPassword,password)) {
                // 封装登录成功返回的信息
                UserLoginTo loginTo = new UserLoginTo();
                BeanUtils.copyProperties(memberEntity,loginTo);
                return loginTo;
            } else {
                return null;
            }
        } else {
            return null;
        }

    }

    private void checkEmailUnique(String email) throws EmailHasExistedException {
        MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("email", email));
        if (memberEntity != null) {
            throw new EmailHasExistedException();
        }
    }

    private void checkUserNameUnique(String userName) throws UserNameHasExistedException {
        MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("userName", userName));
        if (memberEntity != null) {
            throw new UserNameHasExistedException();
        }
    }

}